<?php
/**
 * Description: 翻译推理作业模块
 * User: devforma
 * Date: 16/8/1
 * Time: 12:05
 */

require __DIR__ . '/ProcessExerciseBase.class.php';

class FanyituiliModule extends ProcessExerciseBase {

    /**
     * 设置作业类型
     */
    protected function setExerciseType() {
        $this->data['exercise_type'] = 33;
    }

    /**
     * 设置作业题目
     */
    protected function setExerciseQuestion() {
        global $cmsdb;

        //获取翻译题目信息
        $qids = implode(',', $this->questionIds);
        $sql  = "SELECT * FROM `original_primary_question` WHERE `id` IN ({$qids}) ORDER BY FIELD (`id`, {$qids})";
        $questions = (array)$cmsdb->getResults($sql);

        $data = [];
        foreach ($questions as $fanyi) {

            $question = [
                'fanyi' => [
                    'question_id' => (int)$fanyi->id,
                    'question'    => $fanyi->content,
                    'options'     => explode('#', $fanyi->options),
                    'answer'      => $fanyi->right_answer,
                    'analysis'    => (string)$fanyi->analysis
                ],
                'tuili' => []
            ];

            //获取推理题目信息
            $tuili = (array)$cmsdb->getResults('SELECT * FROM `original_primary_question` WHERE `parent`=:fanyi', [':fanyi' => $fanyi->id]);
            foreach ($tuili as $t) {
                $question['tuili'][] = [
                    'question_id' => (int)$t->id,
                    'question'    => $t->content,
                    'options'     => explode('#', $t->options),
                    'answer'      => $t->right_answer,
                    'analysis'    => (string)$t->analysis
                ];
            }

            $data[] = $question;
        }

        $this->data['question'] = $data;
    }

    /**
     * 设置提交权限
     */
    protected function setSubmitPriv() {
        parent::setSubmitPriv();
    }

    /**
     * 设置用户答案
     */
    protected function setUserAnswer() {
        global $uddb;

        //查询用户做题记录
        $sql = 'SELECT * FROM `history_user_primary_question` WHERE `exercise_id`=:eid AND `user_id`=:uid';
        $records = $uddb->getResults($sql, [':eid' => $this->exerciseId, ':uid' => $this->userId]);
        $answers = [];
        foreach ($records as $r) {
            $answers[$r->question_id] = json_decode($r->user_answer);
        }

        //合并用户答案
        foreach ($this->data['question'] as &$question) {
            $question['fanyi']['user_answer'] = $answers[$question['fanyi']['question_id']]->fanyi;

            foreach ($question['tuili'] as $index => &$tuili) {
                $tuili['user_answer'] = $answers[$question['fanyi']['question_id']]->tuili[$index];
            }
        }
    }

    /**
     * 设置作业的统计信息
     */
    protected function setSummary() {
        parent::setSummary();
    }


    /**
     * 检测提交的参数
     * @param array $args 参数
     * @return bool
     */
    public function validateArgs($args) {
        if (!empty($args['answer_content'])) {
            $this->submitArgs = $args;
            return true;
        }

        return false;
    }

    /**
     * 提交 - 设置作业状态
     */
    protected function setSubmitStatus() {
        if (empty($this->submitArgs['done'])) {
            $this->submitExerciseRecord['status'] = 0; //未完成
        } else {
            if ($this->exerciseInfo->end_time > $this->now)
                $this->submitExerciseRecord['status'] = 1; //已完成
            else
                $this->submitExerciseRecord['status'] = 5; //过期完成
        }
    }

    /**
     * 提交 - 保存答题记录及设置作业统计信息
     */
    protected function saveQuestionRecordsAndSetSubmitSummary() {
        global $uddb;

        $answers = json_decode($this->submitArgs['answer_content']);

        $questionId = $answers->fanyi->question_id;

        //构造答题记录
        $record = array(
            'question_id' => $questionId,
            'exercise_id' => $this->exerciseId,
            'user_id'     => $this->userId,
            'acting_time' => $this->now,
            'user_answer' => $this->submitArgs['answer_content'],
            'is_right'    => 1,
            'status'      => 1,
            'summary'     => ''
        );

        //是否之前提交过了
        $exists = $uddb->getVar('SELECT `id` FROM `history_user_primary_question` WHERE `user_id`=:uid AND `exercise_id`=:eid AND `question_id`=:qid',
            array(':uid' => $this->userId, ':eid' => $this->exerciseId, ':qid' => $questionId));

        //更新or新增
        if ($exists) {
            $uddb->update('history_user_primary_question', $record, array('id' => $exists));
        } else {
            $uddb->insert('history_user_primary_question', $record);
        }

        $this->submitExerciseRecord['summary'] = '';
    }
}